import wikipediaapi
import pandas as pd
import urllib.request
from bs4 import BeautifulSoup
import wikipediaapi
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)


import wikipediaapi
df = pd.read_csv('authors-1980-2021-num-translated-pages.csv')

opere = df.label
wiki_wiki = wikipediaapi.Wikipedia('it')
import time
for opera in opere:
        dik = {}
        time.sleep(5)
        wiki_wiki = wikipediaapi.Wikipedia('it')
        try:
            urlpagina = wiki_wiki.page(opera).fullurl
            print(urlpagina)
        except KeyError:
            print("nothing for", opera)
            pass

        wikipage = urlpagina.split('/wiki/')[1]
        page_py = wiki_wiki.page(wikipage)
        langlinks = page_py.langlinks
        langlinks.update({'it':page_py})

        print(langlinks)
        for k in sorted(langlinks.keys()):
            v = langlinks[k]
            try:
                urlserve = v.fullurl.split('/wiki/')[1]
            except KeyError:
                urlserve = wikipage
                continue
            wiki_wiki2 = wikipediaapi.Wikipedia(k)
            page_py2 = wiki_wiki2.page(urlserve)
            try:
                backlinks = page_py2.backlinks
            except KeyError:
                backlinks = []
                continue
            if dik.get(k) is None:
                dik.update({k:len(backlinks)})
                #dik[k] = len(backlinks)
                print(dik)
                pd.DataFrame.from_dict(dik.items()).to_csv(f"{opera}_inlinks.csv")







